package com.wkedu.repair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wkedu.repair.dao.StudentMapper;
import com.wkedu.repair.dto.StudentDto;
import com.wkedu.repair.entity.Student;
import com.wkedu.repair.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author 港港
 * @version 1.0
 * @date 2021/9/5 10:12 上午
 */
@Transactional
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Override
    public void addStudent(StudentDto studentDto) {
        Student student = new Student();
        student.setStudentId(studentDto.getStudentId());
        student.setMajor(studentDto.getMajor());
        student.setUsername(studentDto.getUsername());
        student.setPassword(studentDto.getPassword());
        student.setName(studentDto.getName());
        studentMapper.insert(student);

    }

    @Override
    public StudentDto getStudentForStudentId(String studentId) {
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("student_id",studentId);
        Student student = studentMapper.selectOne(studentQueryWrapper);
        if (student != null){
            StudentDto studentDto = new StudentDto(
                    student.getName(), student.getUsername(), student.getMajor()
                    , student.getPassword(), student.getStudentId()
            );
            return studentDto;
        }else {
            return null;
        }
    }

    @Override
    public StudentDto getStudentForUsername(String username) {
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("username",username);
        Student student = studentMapper.selectOne(studentQueryWrapper);
        if (student != null){
            StudentDto studentDto = new StudentDto(
                    student.getName(), student.getUsername(), student.getMajor()
                    , student.getPassword(), student.getStudentId()
            );
            return studentDto;
        }else {
            return null;
        }
    }

    @Override
    public void delete(String studentId) {
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("student_id",studentId);
        studentMapper.delete(studentQueryWrapper);
    }
}
